// pages/biz-manage/bill.js
const BizService = require('../../services/biz');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    listLoading: false,
    finished: false,
    billList: [],
    page: 1,
    pageSize: 10,
    
    // 详情弹窗
    showDetail: false,
    currentBill: {},
    payRecords: [],
    tickets: [],
    payRecordsCount: 0,
    ticketsCount: 0,

    billDay: 0,
  },

  /**
   * 确认账单无误
   */
  async onConfirmBill() {
    try {
      const result = await BizService.confirmBill({
        id: this.data.currentBill.id,
        showLoading: true
      });

      if (result.success) {
        wx.showToast({
          title: '确认成功',
          icon: 'success'
        });
        this.refreshData();
        this.onDetailClose();
      }
    } catch (error) {
      console.error('确认账单失败:', error);
      wx.showToast({
        title: '确认失败',
        icon: 'none'
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadBillList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 页面显示时刷新数据
    this.refreshData();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.refreshData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.loadMore();
  },

  /**
   * 加载结算单列表
   */
  async loadBillList() {
    try {
      this.setData({
        listLoading: true
      });

      const result = await BizService.getBillList({
        page: this.data.page,
        pageSize: this.data.pageSize,
        showLoading: false
      });

      if (result.success) {
        const { list, hasMore } = result.data;
        
        this.setData({
          billList: this.data.page === 1 ? list : [...this.data.billList, ...list],
          finished: !hasMore,
          listLoading: false,
          loading: false,
          billDay: result.data.bill_day
        });
      } else {
        wx.showToast({
          title: result.message || '加载失败',
          icon: 'none'
        });
        this.setData({
          listLoading: false,
          loading: false
        });
      }
    } catch (error) {
      console.error('加载结算单列表失败:', error);
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
      this.setData({
        listLoading: false,
        loading: false
      });
    }
  },

  /**
   * 加载更多
   */
  loadMore() {
    if (this.data.listLoading || this.data.finished) {
      return;
    }

    this.setData({
      page: this.data.page + 1
    }, () => {
      this.loadBillList();
    });
  },

  /**
   * 刷新数据
   */
  refreshData() {
    this.setData({
      page: 1,
      finished: false,
      billList: []
    }, () => {
      this.loadBillList();
    });
  },

  /**
   * 点击结算单
   */
  async onBillClick(e) {
    const bill = e.currentTarget.dataset.bill;
    
    try {
      wx.showLoading({
        title: '加载中...'
      });

      const result = await BizService.getBillDetail({
        id: bill.id,
        showLoading: false
      });

      wx.hideLoading();

      if (result.success) {
        const { bill: billDetail, payRecords, tickets, payRecordsCount, ticketsCount } = result.data;
        
        this.setData({
          showDetail: true,
          currentBill: billDetail,
          payRecords: payRecords || [],
          tickets: tickets || [],
          payRecordsCount: payRecordsCount || 0,
          ticketsCount: ticketsCount || 0
        });
      } else {
        wx.showToast({
          title: result.message || '加载详情失败',
          icon: 'none'
        });
      }
    } catch (error) {
      wx.hideLoading();
      console.error('加载结算单详情失败:', error);
      wx.showToast({
        title: '加载详情失败',
        icon: 'none'
      });
    }
  },

  /**
   * 关闭详情弹窗
   */
  onDetailClose() {
    this.setData({
      showDetail: false,
      currentBill: {},
      payRecords: [],
      tickets: [],
      payRecordsCount: 0,
      ticketsCount: 0
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '商家结算单',
      path: '/pages/biz-manage/bill'
    };
  }
});